package com.sky.server.impl;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.OrderDetail;
import com.sky.mapper.OrderDetailMapper;
import com.sky.mapper.AdminOrderMapper;
import com.sky.result.PageResult;
import com.sky.server.AdminOrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AdminOrderServiceImpl implements AdminOrderService {
    @Autowired
    private AdminOrderMapper orderMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Override
    public PageResult getDetails(OrdersPageQueryDTO ordersPageQueryDTO) {


        List<OrderVO> page = orderMapper.getDetails(ordersPageQueryDTO);
        for (OrderVO orderVO : page) {
            List<OrderDetail> orderDetailList = orderDetailMapper.selectByOrdersId(orderVO.getId());
            orderVO.setOrderDetailList(orderDetailList);
        }
        long total = page.size();
        page = page.stream()
                .skip((ordersPageQueryDTO.getPage() - 1) * ordersPageQueryDTO.getPageSize())
                .limit(ordersPageQueryDTO.getPageSize())
                .collect(Collectors.toList());


        PageResult pageResult = new PageResult();
        pageResult.setTotal(total);
        pageResult.setRecords(page);
        log.info(pageResult+"");
        return pageResult;
    }

    @Override
    public OrderVO getDetailsById(Long id) {
        OrderVO orderVO = orderMapper.getDetailsById(id);
        List<OrderDetail> orderDetailList = orderDetailMapper.selectByOrdersId(id);
        orderVO.setOrderDetailList(orderDetailList);
        return orderVO;
    }

    @Override
    public OrderStatisticsVO statistics() {
        OrderStatisticsVO orderStatisticsVO = new OrderStatisticsVO();
        Integer confirmed =  orderMapper.getCinfirmedCount();
        Integer deliveryInProgress =  orderMapper.getDeliveryInProgressCount();
        Integer toBeConfirmed =  orderMapper.getToBeConfirmedCount();
        orderStatisticsVO.setConfirmed(confirmed);
        orderStatisticsVO.setDeliveryInProgress(deliveryInProgress);
        orderStatisticsVO.setToBeConfirmed(toBeConfirmed);
        return orderStatisticsVO;
    }

    @Override
    public void completeById(Long id) {
        orderMapper.completeById(id);
    }

    @Override
    public void confirmById(OrdersConfirmDTO ordersConfirmDTO) {
        orderMapper.confirmById(ordersConfirmDTO);
    }

    @Override
    public void rejectionById(OrdersRejectionDTO ordersRejectionDTO) {
        orderMapper.setRejectionReason(ordersRejectionDTO);
    }

    @Override
    public void deliveryById(Long id) {
        orderMapper.deliveryById(id);
    }

    @Override
    public void cancel(OrdersCancelDTO ordersCancelDTO) {
        orderMapper.setCancelReason(ordersCancelDTO);
    }
}
